package zzzhc.image.recognize.filter;

import zzzhc.image.recognize.Filter;
import zzzhc.image.recognize.RecognizeContext;

public class BorderFilter implements Filter {

	private static final long serialVersionUID = -1436950153312904246L;

	private double rate = 0.1;

	public double getRate() {
		return rate;
	}

	public void setRate(double rate) {
		this.rate = rate;
	}

	public void doFilter(RecognizeContext context) {
		int w = context.getWidth();
		int h = context.getHeight();

		for (int i = 0; i < h; i++) {
			boolean erase = true;
			int count = 0;
			for (int x = 0; x < w; x++) {
				if (context.getValue(x, i) == 0) {
					count++;
				}
			}
			if (count * 1.0 / w > rate) {
				erase = false;
			}
			count = 0;
			if (erase) {
				for (int x = 0; x < w; x++) {
					context.setValue(x, i, 0);
				}
			}

		}

		for (int i = 0; i < w; i++) {
			boolean erase = true;
			int count = 0;

			for (int y = 0; y < h; y++) {
				if (context.getValue(i, y) == 0) {
					count++;
				}
			}
			if (count * 1.0 / h > rate) {
				erase = false;
			}
			if (erase) {
				for (int y = 0; y < h; y++) {
					context.setValue(i, y, 0);
				}
			}
		}
	}
}
